home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / pcplrt.arc / PCPLUSRT.BAS next >
BASIC Source File  |  1988-02-11  |  4KB  |  160 lines

  1. 'QuickBASIC 4.0 SOURCE FILE: PCPLUSRT.BAS
  2. '----------------------------------------------------------------------------
  3. ' PROCOMM+ Directory Sort Utility V1.0
  4. ' (c) 1988 Software Associates of Houston, Texas
  5. ' Written by: Ed Galle, 02/11/88
  6. '----------------------------------------------------------------------------
  7.  
  8. 'Subroutine Declarations
  9. DECLARE FUNCTION DirFile$ ()
  10. DECLARE SUB SortDirEntries (Astart AS INTEGER, Aend AS INTEGER)
  11.  
  12. OPTION BASE 1   'Let's use one as first array element
  13.  
  14. 'Structure of PROCOMM+ Dialing Directory Entry:
  15. TYPE DirRecord
  16.     DirName AS STRING * 25
  17.     Junk AS STRING * 49
  18. END TYPE
  19.  
  20. DIM DFile AS STRING     ' Directory file name
  21. DIM K AS STRING * 1     ' Y/N keystroke
  22. DIM Header AS STRING * 250    ' 250-byte header common to all .DIR files
  23. DIM I AS INTEGER     ' Array Index
  24.  
  25. '200 Entries in a .DIR file
  26. DIM SHARED DirEntry(200) AS DirRecord
  27.  
  28. 'Intro...
  29. CLS
  30. PRINT "PCPLUSRT - PROCOMM+ Dialing Directory Sort Utility"
  31. PRINT "(c) 1988 Software Associates"
  32. PRINT "This program will sort your PROCOMM+ Dialing Directory"
  33. PRINT
  34.  
  35. 'Get .DIR file name from command line...
  36. DFile = DirFile
  37.  
  38. 'Double-check user input...
  39. PRINT "Directory chosen: "; DFile
  40. PRINT
  41. PRINT "Do you want to sort this directory (Y/N)?"
  42. DO
  43.     K = UCASE$(INKEY$)
  44. LOOP WHILE ((K <> "Y") AND (K <> "N"))
  45.  
  46. 'File error handler...(not much!)
  47. ON ERROR GOTO Errorhandle
  48.  
  49. 'Main code:
  50. IF (K = "Y") THEN 'Sort the file
  51.  
  52.     'Open files in binary mode:
  53.     OPEN DFile FOR BINARY AS #1   'Input file
  54.     OPEN "TEMP$$$$.$$$" FOR BINARY AS #2    'Workfile
  55.  
  56.     'Transfer header
  57.     GET #1, , Header
  58.     PUT #2, , Header
  59.  
  60.     'Read directory entries into array, marking blank records
  61.     '  so that they end up at bottom of sorted list
  62.     FOR I = 1 TO 200
  63.         GET #1, , DirEntry(I)
  64.         IF (LEFT$(DirEntry(I).DirName, 1) = " ") THEN
  65.             MID$(DirEntry(I).DirName, 1, 1) = CHR$(255)
  66.         END IF
  67.     NEXT
  68.  
  69.     'Close the old file and rename it .OLD, don't need it anymore:
  70.     CLOSE #1
  71.     NAME DFile AS LEFT$(DFile, INSTR(DFile, ".") - 1) + ".OLD"
  72.  
  73.     'Now sort the directories using Quicksort:
  74.     SortDirEntries 1, 200
  75.  
  76.     'Write sorted table to workfile, unmarking blank records as we go:
  77.     FOR I = 1 TO 200
  78.         IF (LEFT$(DirEntry(I).DirName, 1) = CHR$(255)) THEN
  79.             MID$(DirEntry(I).DirName, 1, 1) = " "
  80.         END IF
  81.         PUT #2, , DirEntry(I)
  82.     NEXT
  83.  
  84.     'Close workfile and rename it as original input file:
  85.     CLOSE #2
  86.     NAME "TEMP$$$$.$$$" AS DFile
  87.  
  88.     'All done
  89.     PRINT
  90.     PRINT "Sort complete!"
  91.     END
  92. ELSE     'User aborted
  93.     PRINT "Sort aborted..."
  94.     END
  95. END IF
  96.  
  97. 'Error handler code:
  98. Errorhandle:
  99.     PRINT "File error occured...aborting"
  100.     CLOSE
  101.     KILL "TEMP$$$$.$$$"
  102.     END
  103.  
  104. 'Function to get the directory filename from command line:
  105.  
  106. 'Quicksort the directory entries:
  107.  
  108. FUNCTION DirFile$
  109.  
  110.     DIM C AS STRING, Lc AS INTEGER, I AS INTEGER, M AS STRING * 1
  111.  
  112.     C = COMMAND$
  113.     Lc = LEN(C)
  114.      
  115.     IF (Lc) THEN
  116.         FOR I = 1 TO Lc
  117.             M = MID$(C, I, 1)
  118.             IF ((M = " ") OR (M = CHR$(9))) THEN EXIT FOR
  119.         NEXT
  120.         DirFile$ = LEFT$(C, I - 1)
  121.     ELSE
  122.         PRINT
  123.         PRINT "Usage: PCPLUSRT <directory file>"
  124.         PRINT
  125.         PRINT " <directory file> = PROCOMM+ directory file name"
  126.         END
  127.     END IF
  128.  
  129. END FUNCTION
  130.  
  131. SUB SortDirEntries (Astart AS INTEGER, Aend AS INTEGER)
  132.  
  133.     DIM Pivot AS STRING * 25
  134.     DIM Low AS INTEGER, High AS INTEGER
  135.  
  136.     Low = Astart
  137.     High = Aend
  138.     Pivot = DirEntry((High + Low) \ 2).DirName
  139.     
  140.     DO WHILE (Low <= High)
  141.         DO WHILE (DirEntry(Low).DirName < Pivot)
  142.             Low = Low + 1
  143.         LOOP
  144.         DO WHILE (DirEntry(High).DirName > Pivot)
  145.             High = High - 1
  146.         LOOP
  147.         IF (Low < High) THEN
  148.             SWAP DirEntry(Low), DirEntry(High)
  149.             Low = Low + 1
  150.             High = High - 1
  151.         ELSEIF (Low = High) THEN
  152.             Low = Low + 1
  153.         END IF
  154.     LOOP
  155.     IF (Astart < High) THEN SortDirEntries Astart, High
  156.     IF (Low < Aend) THEN SortDirEntries Low, Aend
  157.  
  158. END SUB
  159.  
  160.